import 'package:flutter/material.dart';

import 'audio_player.dart';

///全局悬浮播放器
class OverlayAudioPlayer {
  ///当前播放状态
  static OverlayAudioPlayerState currentState = OverlayAudioPlayerState.none;

  ///当前播放的音频地址
  static String currentAudioUrl;

  ///监听播放状态的变化，主要是 暂停、继续和播放完成
  static OnStateChanged _onPlayerStateListener;

  static OverlayState _overlayState;
  static OverlayEntry _playerEntry;
  static double _x = 15;
  static double _y = 80;

  ///当前标题是否可用
  static bool isPlayTitleVisible;

  ///展示音频播放器
  static showAudioPlayer(BuildContext context,
      {bool isTTSPlayer, String audioUrl, String ttsText, OnStateChanged onPlayerStateListener}) {
    if (currentState != OverlayAudioPlayerState.none) {
      closeAudioPlayer(context);
    }
    _onPlayerStateListener = onPlayerStateListener;
    isPlayTitleVisible = true;
    if (null != _playerEntry) {
      _playerEntry.remove();
      _playerEntry = null;
    }
    _x = 15;
    _y = 80;

    _playerEntry = new OverlayEntry(builder: (context) {
      var realPlayer = AudioPlayerWidget(
          closeOnTap: () {
            closeAudioPlayer(context);
          },
          audioUrl: audioUrl,
          isTTSPlayer: isTTSPlayer,
          ttsText: ttsText,
          showTitle: isPlayTitleVisible,
          playStateChanged: (playState) {
            if (playState) {
              currentState = OverlayAudioPlayerState.Playing;
              if (null != _onPlayerStateListener) _onPlayerStateListener(OverlayAudioPlayerState.Playing);
            } else {
              currentState = OverlayAudioPlayerState.Paused;
              if (null != _onPlayerStateListener) _onPlayerStateListener(OverlayAudioPlayerState.Paused);
            }
          });

      return Positioned(
        left: _x,
        bottom: _y,
        child: new GestureDetector(
          child: realPlayer,
          onVerticalDragUpdate: (detail) {
            // ignore: invalid_use_of_protected_member
            _overlayState.setState(() {
              var currentY = MediaQuery.of(context).size.height - detail.globalPosition.dy;
              if (currentY < 3) {
                currentY = 3;
              }
              if (currentY > MediaQuery.of(context).size.height - 2 * 44) {
                currentY = MediaQuery.of(context).size.height - 2 * 44;
              }

              _y = currentY;
            });
          },
        ),
//          child: LongPressDraggable(
//            axis: Axis.vertical,
//            child: realPlayer,
//            feedback: wPlayer,
//            childWhenDragging: wPlayer,
//            onDragStarted: () {
//
//            },
//            onDragEnd: (details) {
//              RenderBox renderBox = anchorKey.currentContext.findRenderObject();
//              var offset = renderBox.localToGlobal(Offset.zero);
//              debugPrint("onDragEnd----------->   ${offset.dy}");
//              debugPrint("onDragEnd 2----------->   ${details.offset.dy}");
////              overlayState.setState(() {
////                y = details.offset.dy;
////              });
//            },
//            onDraggableCanceled: (velocity, offset) {
//              RenderBox renderBox = anchorKey.currentContext.findRenderObject();
//              var _offset = renderBox.localToGlobal(Offset.zero);
//              debugPrint("----------->   ${_offset.dy}");
//              debugPrint("onDraggableCanceled offset  ${offset.dx}   ${offset.dy}");
////              overlayState.setState(() {
////                y = offset.dy;
////              });
//            },
//          )
      );
    });
    _overlayState = Overlay.of(context);
    _overlayState.insert(_playerEntry);
//    _onPlayerStateListener?.onStateChanged(OverlayAudioPlayerState.Playing);
//    currentState = OverlayAudioPlayerState.Playing;
  }

  ///释放当前的监听
  static releasePlayerStateListener() {
    if (null != _onPlayerStateListener) _onPlayerStateListener = null;
  }

  ///隐藏音频播放器
  static closeAudioPlayer(BuildContext context) {
    currentState = OverlayAudioPlayerState.none;
    if (null != _onPlayerStateListener) {
      _onPlayerStateListener(currentState);
      _onPlayerStateListener = null;
    }
    if (null != _playerEntry) {
      _playerEntry.remove();
      _playerEntry = null;
    }
  }

  static setTitleVisible(bool isVisible) {
    // ignore: invalid_use_of_protected_member
    _overlayState.setState(() {
      isPlayTitleVisible = isVisible;
    });
  }
}

enum OverlayAudioPlayerState {
  ///正在播放
  Playing,

  ///已经暂停
  Paused,

  ///没有播放过
  none
}

typedef OnStateChanged(OverlayAudioPlayerState state);
